
using UnityEngine;


public class InputCtl : MonoBehaviour
{
    
    public Vector2 lastTempIndex = Vector2.zero;
    private GameObject bg;
    GameObject posStatus;
    public GameObject towerPrefab;
    private MapCell mouseOverCell;
    // Start is called before the first frame update
    void Start()
    {
        posStatus = getPosStatusObj();
    }

    // Update is called once per frame
    void Update()
    {
        //选择防御塔检测
        checkSelectTower();
        //取消选择防御塔检测
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            if (GameManager.Instance.selectedTower())
            {
                GameManager.Instance.cancelSelect();
            }
        }
        //建造防御塔检测
        buildTowerCheck();
    }

    private void buildTowerCheck()
    {
        if (Input.GetMouseButtonUp(0))
        {
            if (mouseOverCell != null && mouseOverCell.canBuildTower())//建造塔
            {
                //TODO 扣钱，钱够不够在选择的时候进行判断
                int selectTowerId = GameManager.Instance.SelectTowerId;
                //根据towerId查找配置,根据预制体名称找到预制体并实例话
                //资源管理器加载预制体
                GameObject towerInstance = GameObject.Instantiate(towerPrefab, mouseOverCell.pos, Quaternion.identity);
                // towerInstance.transform.parent = 
                towerInstance.transform.parent = getTowerRoot().transform;
                mouseOverCell.tower = towerInstance;
                Debug.Log("造塔！");
            }
        }
    }

    private void checkSelectTower()
    {
        if (GameManager.Instance.selectedTower())
        {
            //TODO 检查鼠标是否在uitoolkit的ui上

            //鼠标所在
            var indexByPos = GameManager.Instance.getIndexByPos(Input.mousePosition);
            if (indexByPos.Equals(lastTempIndex))
            {
                return;
            }
            // Debug.Log(indexByPos);
            this.lastTempIndex = indexByPos;
            var screenPointToRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            var raycast = Physics.Raycast(screenPointToRay,out hit,LayerMask.GetMask("Bg"));
            if (raycast)
            {
                var hitPoint = hit.point;
                // Debug.Log(hitPoint);
                Vector2 cellIndex = GameManager.Instance.getIndexByPos(hitPoint);
                
                mouseOverCell = GameManager.Instance.getMapCell(cellIndex);
                if (mouseOverCell == null)
                {
                    return;
                }

                Vector3 mapCellPos3 = new Vector3(mouseOverCell.pos.x, mouseOverCell.pos.y, 0);
                posStatus.transform.position = mapCellPos3;
                SpriteRenderer spriteRenderer = posStatus.GetComponent<SpriteRenderer>();
                if (mouseOverCell.isRoad || mouseOverCell.tower!=null)
                {
                    spriteRenderer.material.color = new Color32(222, 10, 50,255);
                }
                else
                {
                    spriteRenderer.material.color = new Color32(46, 236, 98,255);
                }

                posStatus.SetActive(true);
                
            }
        }
        else
        {
            posStatus.SetActive(false);
            if (mouseOverCell != null)
            {
                mouseOverCell = null;
            }
        }
    }

    public GameObject getPosStatusObj()
    {
        if (this.bg == null)
        {
            this.bg = GameObject.FindWithTag("Bg");

        }
        return  this.bg.transform.GetChild(2).gameObject;
    }
    public GameObject getTowerRoot()
    {
        if (this.bg == null)
        {
            this.bg = GameObject.FindWithTag("Bg");

        }
        return  this.bg.transform.GetChild(1).gameObject;
    }
}
